﻿using SQLite;


namespace FireChat.Share.Database.Util;


/// <summary>
/// 数据库管理类
/// </summary>
internal static class DatabaseManager
{
    /// <summary>
    /// 所有数据库连接实例
    /// </summary>
    private static Dictionary<string, SQLiteConnection> _INSTANCES = new(StringComparer.OrdinalIgnoreCase);



    /// <summary>
    /// 默认数据库名称
    /// </summary>
    public const string DefaultDatabaseName = "database.db";

    /// <summary>
    /// 数据文件目录
    /// </summary>
    public const string DatabaseFolderName = "database";

    /// <summary>
    /// 默认数据库
    /// </summary>
    public static SQLiteConnection Default => GetOrCreate(DefaultDatabaseName);



    /// <summary>
    /// 获取或创建Sqlite连接
    /// </summary>
    public static SQLiteConnection GetOrCreate(string databaseName)
    {
        if (_INSTANCES.ContainsKey(databaseName))
        {
            return _INSTANCES[databaseName];
        }

        string db_path = Path.Combine(DatabaseFolderName, databaseName);


        if (!Directory.Exists(DatabaseFolderName))
        {
            Directory.CreateDirectory(DatabaseFolderName);
        }

        SQLiteConnection connection = new(db_path);
        _INSTANCES[databaseName] = connection;

        return connection;
    }
}
